package com.evil.enterprise.controller;

import com.evil.account.api.RemoteEnterpriseService;
import com.evil.account.pojo.dto.enterprise.EnterpriseBaseInfoResp;
import com.evil.common.account.bo.AuthUserBO;
import com.evil.common.account.dto.enterprise.*;
import com.evil.common.account.dto.user.UserBaseRespDTO;
import com.evil.common.core.entity.QueryModel;
import com.evil.common.core.entity.R;
import com.evil.common.core.enums.SwitchEnum;
import com.evil.common.file.dto.FileRespDTO;
import com.evil.common.file.dto.UploadReqDTO;
import com.evil.common.file.constant.OssConstant;
import com.evil.common.file.enums.BusTypeEnum;
import com.evil.file.api.RemoteFileService;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 企业接口
 *
 * @author liyang
 * @date 2022-08-08 00:00
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/enterprise")
public class EnterpriseController {

    private final RemoteEnterpriseService remoteEnterpriseService;
    private final RemoteFileService remoteFileService;

    /**
     * 切换登录企业
     *
     * @param switchEnterpriseReqDTO switchEnterpriseReqDTO
     * @return R
     */
    @PostMapping("/login/switch")
    public R<AuthUserBO> modifyLoginEnterprise(@Validated @RequestBody SwitchEnterpriseReqDTO switchEnterpriseReqDTO) {
        return R.success(remoteEnterpriseService.modifyLoginEnterprise(switchEnterpriseReqDTO.getEnterpriseId()));
    }

    /**
     * 当前登录用户企业信息
     *
     * @return R
     */
    @PostMapping("/info-login")
    private R<EnterpriseBaseInfoResp> loginEnterpriseInfo() {
        return R.success(remoteEnterpriseService.loginEnterpriseInfo());
    }

    /**
     * 修改当前登录用户企业信息
     *
     * @param enterpriseReqDTO enterpriseReqDTO
     * @return R
     */
    @PostMapping("/info-login/modify")
    private R<?> modifyLoginEnterpriseInfo(@Validated @RequestBody EnterpriseReqDTO enterpriseReqDTO) {
        remoteEnterpriseService.modifyLoginEnterpriseInfo(enterpriseReqDTO);
        return R.success();
    }

    /**
     * 上传企业logo
     *
     * @param uploadReqDTO uploadReqDTO
     * @return R
     */
    @PostMapping("/logo/upload")
    public R<FileRespDTO> uploadEnterpriseLogo(@Validated UploadReqDTO uploadReqDTO) {
        uploadReqDTO.setType(BusTypeEnum.PORTRAIT.getId());
        uploadReqDTO.setEnterpriseId(null);
        uploadReqDTO.setUserId(null);
        uploadReqDTO.setAllowMaxSize(OssConstant.UPLOAD_USER_PORTRAIT_SIZE);
        uploadReqDTO.setAllowContentType(OssConstant.UPLOAD_USER_PORTRAIT_TYPE);
        return R.success(remoteFileService.uploadFile(uploadReqDTO));
    }

    /**
     * 修改企业logo
     *
     * @param modifyEnterpriseLogoReqDTO modifyEnterpriseLogoReqDTO
     * @return R
     */
    @PostMapping("/logo/modify")
    public R<?> modifyEnterpriseLogo(@Validated @RequestBody ModifyEnterpriseLogoReqDTO modifyEnterpriseLogoReqDTO) {
        modifyEnterpriseLogoReqDTO.setUserId(null);
        modifyEnterpriseLogoReqDTO.setEnterpriseId(null);
        remoteEnterpriseService.modifyEnterpriseLogo(modifyEnterpriseLogoReqDTO);
        return R.success();
    }

    /**
     * 企业管理员列表
     *
     * @param queryModel queryModel
     * @return R
     */
    @PostMapping("/manager/list")
    public R<PageInfo<UserBaseRespDTO>> enterpriseManagerList(@Validated @RequestBody QueryModel<EnterpriseManagerPageReqDTO> queryModel) {
        queryModel.getParam().setEnterpriseId(null);
        queryModel.getParam().setIsManager(SwitchEnum.YES.getId());
        return R.success(remoteEnterpriseService.enterpriseManagerList(queryModel));
    }

    /**
     * 新增企业管理员
     *
     * @param handleEnterpriseManagerReqDTO handleEnterpriseManagerReqDTO
     * @return R
     */
    @PostMapping("/manager/add")
    public R<?> addEnterpriseManager(@Validated @RequestBody HandleEnterpriseManagerReqDTO handleEnterpriseManagerReqDTO) {
        handleEnterpriseManagerReqDTO.setEnterpriseId(null);
        handleEnterpriseManagerReqDTO.setIsManager(SwitchEnum.YES.getId());
        remoteEnterpriseService.handleEnterpriseManager(handleEnterpriseManagerReqDTO);
        return R.success();
    }

    /**
     * 移除企业管理员
     *
     * @param handleEnterpriseManagerReqDTO handleEnterpriseManagerReqDTO
     * @return R
     */
    @PostMapping("/manager/remove")
    public R<?> removeEnterpriseManager(@Validated @RequestBody HandleEnterpriseManagerReqDTO handleEnterpriseManagerReqDTO) {
        handleEnterpriseManagerReqDTO.setEnterpriseId(null);
        handleEnterpriseManagerReqDTO.setIsManager(SwitchEnum.NO.getId());
        remoteEnterpriseService.handleEnterpriseManager(handleEnterpriseManagerReqDTO);
        return R.success();
    }
}
